#include "serverwindow.h"
#include "ui_serverwindow.h"
#include "serveroperation.h"
#include <QDateTime>
#include <QThreadPool>

ServerWindow::ServerWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ServerWindow)
{
    ui->setupUi(this);

    ui->tabWidget->setCurrentIndex(0);
    // 创建任务对象 -> 检测并建立新连接的任务对象
    AcceptConn* acc = new AcceptConn;
    // 将任务对象添加到线程池中, 线程池就会拿出一个线程处理添加的任务
    QThreadPool::globalInstance()->start(acc);
    connect(acc, &AcceptConn::sendMsg, this, [=](QString msg)
    {
        QDateTime tm = QDateTime::currentDateTime();
        QString curTm = tm.toString("yyyy-MM-dd hh:mm:ss");
        // 显示msg到窗口中即可
        ui->msg->append(curTm + ": " + msg);
    });

    // 初始化设置窗口数据
    connect(ui->tabWidget, &QTabWidget::currentChanged, this, [=](int index)
    {
        if(index == 1)
        {
            ui->setTab->showPage(ConfigSet::Server);
        }
    });

    // 处理当前窗口对象发出的 closeWindowA() 信号
    connect(this, &ServerWindow::closeWindowA, acc, &AcceptConn::stop);
}

ServerWindow::~ServerWindow()
{
    delete ui;
}

void ServerWindow::closeEvent(QCloseEvent *event)
{
    emit closeWindowA();
}
